package service

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	__ "user_srv/basic/proto"
	"user_srv/handler/models"
	"user_srv/pkg"
)

type Server struct {
	__.UnimplementedUserServer
}

// 创建订单
func (s *Server) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {
	order := models.Order{
		OrderSn:     uuid.NewString(),
		PatientId:   uint(in.PatientId),
		DoctorId:    uint(in.DoctorId),
		IllnessType: in.IllnessType,
		OrderPrice:  float64(in.OrderPrice),
	}
	err := order.CreateOrder()
	if err != nil {
		return nil, fmt.Errorf("创建订单失败")
	}
	fmt.Println(pkg.AliPay(order.OrderSn, fmt.Sprintf("%.2f", order.OrderSn)))

	return &__.CreateOrderResp{
		OrderId: int64(order.Id),
	}, nil
}

// 添加医生
func (s *Server) CreateDoctor(_ context.Context, in *__.CreateDoctorReq) (*__.CreateDoctorResp, error) {
	doctor := models.Doctor{
		DoctorName:       in.DoctorName,
		DoctorUrl:        in.DoctorUrl,
		HospitalPosition: in.HospitalPosition,
		HospitalType:     in.HospitalType,
	}
	err := doctor.CreateDoctor()
	if err != nil {
		return nil, fmt.Errorf("添加医生失败")
	}
	return &__.CreateDoctorResp{
		DoctorId: int64(doctor.Id),
	}, nil
}

// 医生详情
func (s *Server) DetailDoctor(_ context.Context, in *__.DetailDoctorReq) (*__.DetailDoctorResp, error) {
	var doctor models.Doctor
	doctor.Id = uint(in.DoctorId)
	err := doctor.DetailDoctor()
	if err != nil {
		return nil, fmt.Errorf("获取医生信息失败")
	}
	if doctor.Id == 0 {
		return nil, fmt.Errorf("该医生未存在")
	}
	return &__.DetailDoctorResp{
		DoctorName:       doctor.DoctorName,
		DoctorUrl:        doctor.DoctorUrl,
		HospitalPosition: doctor.HospitalPosition,
		HospitalType:     doctor.HospitalType,
	}, nil
}

// 修改状态
func (s *Server) UpdateReservation(_ context.Context, in *__.UpdateReservationReq) (*__.UpdateReservationResp, error) {
	var reservation models.Reservation
	reservation.Id = uint(in.ReservationId)
	reservation.ReservationStatus = uint(in.ReservationStatus)
	err := reservation.UpdateStatus()
	if err != nil {
		return nil, fmt.Errorf("修改状态失败")
	}
	return &__.UpdateReservationResp{
		ReservationId: int64(reservation.Id),
	}, nil
}
